package com.charge.web;

import com.charge.domain.ColorModel;
import com.charge.domain.User;
import com.charge.domain.UserIncomeType;
import com.charge.domain.UserSpendingType;
import com.charge.service.ColorService;
import com.charge.service.IncomeService;
import com.charge.service.SpendingService;
import com.charge.util.Const;
import com.charge.util.WebResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Description :
 * Author : 91658
 * Date : 2018/6/21 22:11
 **/
@Controller
@RequestMapping("/income")
public class IncomeController {

    @Autowired
    private IncomeService incomeService;

    @Autowired
    private ColorService colorService;

    @RequestMapping("/income_manage")
    public String incomeManage(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");
        UserIncomeType incomeType = new UserIncomeType();
        incomeType.setUserId(user.getId());
        List<UserIncomeType> userIncomeTypeList = incomeService.selectIncomeList(incomeType);
        request.setAttribute("incomeList", userIncomeTypeList);
        return "/income/income_manage";
    }

    @RequestMapping("/add_income_frame")
    public String addIncomeFrame(HttpServletRequest request) {
        List<ColorModel> colorModelList = colorService.selectColorList();
        request.setAttribute("colorList", colorModelList);
        return "/income/add_income";
    }

    @RequestMapping("/add_income")
    @ResponseBody
    public WebResponse<String> addSpending(UserIncomeType incomeType, HttpSession session) {
        User user = (User) session.getAttribute("user");
        incomeType.setUserId(user.getId());
        incomeService.insertIncome(incomeType);
        return WebResponse.createBySuccess(Const.SUCCESS, "");
    }

}
